<template>
  <div class="comment_placeholder">
    <div
      class="comment_cover"
      v-show="isActive"
      @click="isActive=false"
    ></div>
    <div class="comment">
      <!-- 形态1. 简单的展示 -->
      <div
        class="type1"
        v-show="!isActive"
      >
        <input
          type="text"
          placeholder="写跟帖"
          v-model="content"
          @focus='handlerFocus'
        >
        <router-link
          :to="`/comment/${article.id}`"
          class="icon-group"
        >
          <i class="iconfont iconpinglun-"></i>
          <em>102</em>
        </router-link>
        <!-- 收藏按钮 -->
        <span
          class="icon-group"
          @click="collectFn"
        >
          <i
            class="iconfont iconshoucang"
            :class="{
          star: article.has_star
        }"
          ></i>
        </span>
        <router-link
          to="/user_comment"
          class="icon-group"
        >
          <i class="iconfont iconfenxiang"></i>
        </router-link>
      </div>
      <!-- 形态2. 文本域，用户课室输入 -->
      <div
        class="type2"
        v-show="isActive"
      >
        <!-- Vant 也有组件 -->
        <textarea
          class="textarea"
          placeholder="回复: @火星网友"
          v-model.trim="content"
          ref="commentArea"
        ></textarea>
        <div class="btn-group">
          <span
            class="btn"
            @click="cancel"
          >取消</span>
          <span
            class="btn"
            @click="sendComment"
          >发送</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { publishPostCommentAPI, updatePostStarByIdAPI } from "../apis/news";

export default {
  props: {
    article: Object,
  },
  data() {
    return {
      isActive: false,
      content: "",
    };
  },
  methods: {
    // 单击取消
    cancel() {
      // console.log('我单击了取消');
      this.isActive = false;
      // 将当前评论对象清空,这句代码会报错
      // this.commentObj = null
      // 只能由父组件进行处理
    },
    // 单击输入框
    handlerFocus() {
      // 弹出输入框--文本域
      this.isActive = true;
      // 聚焦
      this.$nextTick(() => {
        this.$refs.commentArea.focus();
      });
    },
    async collectFn() {
      // console.log("实现文章收藏", this.article.id);
      // 调用接口
      const res = await updatePostStarByIdAPI(this.article.id);
      // console.log(res);
      const { message } = res.data;
      // 提示操作状态
      if (message === "收藏成功") {
        this.$toast.success(message);
      } else {
        this.$toast.fail(message);
      }
      // 更新页面绑定的类名
      this.article.has_star = !this.article.has_star;
    },
    async sendComment() {
      if (this.content === "") return this.$toast.fail("请输入评论内容");
      const res = await publishPostCommentAPI(this.article.id, {
        content: this.content,
        parent_id: null,
      });
      this.$toast.success(res.data.message);
      this.isActive = false;
      this.content = "";
      console.log(res);
    },
  },
};
</script>

<style lang="less" scoped>
.comment_cover {
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
}
.comment_placeholder {
  height: 100px;
}
.comment {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #f2f2f2;
}
.star {
  color: red;
}
.type1 {
  display: flex;
  height: 50px;
  align-items: center;
  padding: 0 10px;
  input {
    flex: 1;
    height: 30px;
    border-radius: 15px;
    background-color: #d7d7d7;
    border: none;
    padding: 0 10px;
    margin: 10px;
  }
  .icon-group {
    width: 40px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    .iconfont {
      font-size: 20px;
    }
    em {
      background-color: red;
      color: #fff;
      position: absolute;
      right: 0px;
      top: 0;
      font-size: 12px;
      border-radius: 15px;
      padding: 0 2px;
    }
  }
}
.type2 {
  display: flex;
  padding: 10px;
  .textarea {
    height: 80px;
    background-color: #d7d7d7;
    border-radius: 5px;
    flex: 1;
    padding: 5px;
  }

  .btn-group {
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .btn {
      width: 60px;
      height: 25px;
      background-color: red;
      color: #fff;
      border-radius: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>